<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>手机号信息查询</title>
<style>
    body {
        font-family: "Microsoft YaHei", Arial, sans-serif;
        background: #f0f2f5;
        padding: 30px;
    }
    .container {
        max-width: 500px;
        margin: auto;
        background: #fff;
        padding: 20px 30px;
        border-radius: 10px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
    h2 {
        text-align: center;
        color: #333;
    }
    .input-group {
        display: flex;
        gap: 10px;
    }
    input {
        flex: 1;
        padding: 10px;
        font-size: 16px;
        border: 1px solid #ddd;
        border-radius: 5px;
        outline: none;
    }
    button {
        width: 100px;
        padding: 10px;
        font-size: 16px;
        background: #1890ff;
        color: white;
        border: none;
        border-radius: 5px;
        cursor: pointer;
    }
    button:disabled {
        background: #ccc;
        cursor: not-allowed;
    }
    button:hover:enabled {
        background: #40a9ff;
    }
    #loading {
        text-align: center;
        color: #888;
        margin-top: 15px;
    }
    .progress-container {
        width: 100%;
        background-color: #eee;
        border-radius: 5px;
        overflow: hidden;
        margin-top: 5px;
        height: 10px;
    }
    .progress-bar {
        height: 10px;
        width: 0%;
        transition: width 1s linear, background-color 1s linear;
    }
    #cooldownText {
        text-align: center;
        font-size: 14px;
        color: #555;
        margin-top: 10px;
    }
    .record-box {
        background: #f9f9f9;
        border: 1px solid #ddd;
        padding: 10px;
        border-radius: 5px;
        line-height: 1.6;
        margin-top: 15px;
        white-space: pre-wrap;
    }
</style>
</head>
<body>
<div class="container">
    <h2>📱 手机号信息查询</h2>
    <div class="input-group">
        <input type="text" id="phone" placeholder="请输入手机号" value="17605275327">
        <button id="queryBtn" onclick="query()">查询</button>
    </div>
    <div id="loading"></div>
    <div id="progressWrapper" style="display:none;">
        <div id="cooldownText"></div>
        <div class="progress-container">
            <div class="progress-bar" id="progressBar"></div>
        </div>
    </div>
    <div id="result"></div>
</div>

<script>
let cooldown = 60;

window.onload = function() {
    const lastQueryTime = localStorage.getItem('lastQueryTime');
    const savedCooldown = localStorage.getItem('cooldown');
    if (lastQueryTime && savedCooldown) {
        cooldown = parseInt(savedCooldown);
        const elapsed = Math.floor((Date.now() - parseInt(lastQueryTime)) / 1000);
        if (elapsed < cooldown) {
            startCooldown(cooldown - elapsed);
        }
    }
};

document.getElementById('phone').addEventListener('keydown', function(e) {
    if (e.key === 'Enter') {
        query();
    }
});

function startCooldown(seconds) {
    const btn = document.getElementById('queryBtn');
    const progressWrapper = document.getElementById('progressWrapper');
    const progressBar = document.getElementById('progressBar');
    const cooldownText = document.getElementById('cooldownText');

    btn.disabled = true;
    let timeLeft = seconds;
    btn.textContent = `等待 ${timeLeft}s`;

    progressWrapper.style.display = 'block';
    progressBar.style.width = '0%';
    cooldownText.textContent = `⏳ 冷却中…剩余 ${timeLeft} 秒`;

    const total = seconds;
    const timer = setInterval(() => {
        timeLeft--;
        btn.textContent = `等待 ${timeLeft}s`;
        cooldownText.textContent = `⏳ 冷却中…剩余 ${timeLeft} 秒`;

        let percent = ((total - timeLeft) / total) * 100;
        progressBar.style.width = percent + '%';
        let hue = (timeLeft / total) * 120; 
        progressBar.style.backgroundColor = `hsl(${hue}, 80%, 50%)`;

        if (timeLeft <= 0) {
            clearInterval(timer);
            btn.disabled = false;
            btn.textContent = '查询';
            progressWrapper.style.display = 'none';
            localStorage.removeItem('lastQueryTime');
            localStorage.removeItem('cooldown');
        }
    }, 1000);
}

function query() {
    const phone = document.getElementById('phone').value.trim();
    const phonePattern = /^1[3-9]\d{9}$/;

    if (!phonePattern.test(phone)) {
        alert('请输入正确的 11 位中国大陆手机号');
        return;
    }

    const lastQueryTime = localStorage.getItem('lastQueryTime');
    const savedCooldown = localStorage.getItem('cooldown');
    if (lastQueryTime && savedCooldown) {
        const elapsed = Math.floor((Date.now() - parseInt(lastQueryTime)) / 1000);
        if (elapsed < parseInt(savedCooldown)) {
            alert(`请等待 ${parseInt(savedCooldown) - elapsed} 秒后再试`);
            return;
        }
    }

    const url = `http://api.qnm6.top/api/sgzh/?token=apione1xiaoguo&msg=${phone}`;
    document.getElementById('loading').textContent = '🔍 查询中，请稍候...';
    document.getElementById('result').innerHTML = '';

    fetch(url)
        .then(response => response.json())
        .then(data => {
            document.getElementById('loading').textContent = '';

            const isSuccess = (data.code === 200) || 
                              (typeof data.message === 'string' && data.message.includes('成功'));

            if (isSuccess && data.shuju) {
                cooldown = cooldown || 60;
                localStorage.setItem('lastQueryTime', Date.now());
                localStorage.setItem('cooldown', cooldown);
                startCooldown(cooldown);

                // 自动美化 shuju 字段
                let records = data.shuju.trim().split(/\n\s*\n/); // 按空行分割记录
                let html = '';
                records.forEach((rec, idx) => {
                    let lines = rec.split('\n');
                    html += `<div class="record-box"><strong>记录 ${idx+1}</strong><br>`;
                    lines.forEach(line => {
                        html += `${line}<br>`;
                    });
                    html += '</div>';
                });
                document.getElementById('result').innerHTML = html;

            } else {
                document.getElementById('result').innerHTML =
                    `<p style="color:red;">❌ 查询失败：${data.message || '未知错误'}</p>`;

                const match = data.message && data.message.match(/等待\s*(\d+)\s*秒/);
                if (match) {
                    cooldown = parseInt(match[1]);
                    localStorage.setItem('lastQueryTime', Date.now());
                    localStorage.setItem('cooldown', cooldown);
                    startCooldown(cooldown);
                }
            }
        })
        .catch(err => {
            document.getElementById('loading').textContent = '';
            document.getElementById('result').innerHTML =
                `<p style="color:red;">⚠️ 请求出错：${err}</p>`;
        });
}
</script>
</body>
</html>
